package cn.com.callback;

import cn.com.elite.EliteBean;
import cn.com.service.GeneralService;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

class apiQueueNavigation implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiQueueNavigation.class);

    private static final String DATA_SOURCE_NAME = "dataSource";

    //查询全部的标题包含类型
    private static final String QUERY_SQL_FOR_QUEUE_NAVIGATION = "SELECT ICON, NAME, QUEUE_ID from CHAT_QUEUE_SERVICE WHERE 1 = 1 ORDER BY ORDER_ID ";

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        log.debug("-------START GET QUEUE_NAVIGATION-------");
        JSONObject jsonRtJSON = new JSONObject();
        response.setCharacterEncoding("UTF-8");
        DataSource dataSource = (DataSource) GeneralService.wac.getBean(DATA_SOURCE_NAME);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        JSONObject questions = new JSONObject();
        List<Map<String, String>> listForQueueNavagation = jdbcTemplate.query(QUERY_SQL_FOR_QUEUE_NAVIGATION, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
            }
        }, new ResultSetExtractor<List<Map<String, String>>>() {
            @Override
            public List<Map<String, String>> extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<Map<String, String>> result = new ArrayList<Map<String, String>>();
                while (rs.next()) {
                    Map<String, String> mapRs = new HashMap<>();
                    mapRs.put("icon", rs.getString(1));
                    mapRs.put("name", rs.getString(2));
                    mapRs.put("queue", rs.getString(3));
                    result.add(mapRs);
                }
                return result;
            }
        });
        jsonRtJSON.put("status", "success");
        jsonRtJSON.put("title", "请选择您的咨询业务类型");
        jsonRtJSON.put("data", listForQueueNavagation);
        log.debug("打印最终 JSON QUESTION ALL：" + jsonRtJSON.toString());
        return jsonRtJSON.toString();
    }
}